<!DOCTYPE html>
<html lang="zh-CN">
    <!-- title -->




<!-- keywords -->




<head><meta name="generator" content="Hexo 3.9.0">
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <meta name="author" content="XQ">
    <meta name="renderer" content="webkit">
    <meta name="copyright" content="XQ">
    
    <meta name="keywords" content="桌面,iOS,Android,RN,Web,晴风">
    
    <meta name="description" content>
    <meta name="description" content="1. OC 内存管理1. 堆 和 栈栈：编译器与操作系统使用，自动释放，入参或者局部变量，先进后出，不会出现内存碎片。增长方式：向低地址 堆：程序员动态分配，非基本数据类型，程序结束是OS回收，但是分配的内存无用后不释放会产生内存泄漏。动态分配，会产生碎片。增长方式：向高地址。 2. 僵尸对象 空指针 野指针僵尸对象：已经被销毁的对象，内存区域无法再被使用。 空指针：指针无指向，默认nil（0）。">
<meta name="keywords" content="桌面">
<meta property="og:type" content="article">
<meta property="og:title" content="内存管理">
<meta property="og:url" content="https://blog.xq99.org/posts/2.内存管理/index.html">
<meta property="og:site_name" content="晴风的博客">
<meta property="og:description" content="1. OC 内存管理1. 堆 和 栈栈：编译器与操作系统使用，自动释放，入参或者局部变量，先进后出，不会出现内存碎片。增长方式：向低地址 堆：程序员动态分配，非基本数据类型，程序结束是OS回收，但是分配的内存无用后不释放会产生内存泄漏。动态分配，会产生碎片。增长方式：向高地址。 2. 僵尸对象 空指针 野指针僵尸对象：已经被销毁的对象，内存区域无法再被使用。 空指针：指针无指向，默认nil（0）。">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2020-12-03T06:18:13.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="内存管理">
<meta name="twitter:description" content="1. OC 内存管理1. 堆 和 栈栈：编译器与操作系统使用，自动释放，入参或者局部变量，先进后出，不会出现内存碎片。增长方式：向低地址 堆：程序员动态分配，非基本数据类型，程序结束是OS回收，但是分配的内存无用后不释放会产生内存泄漏。动态分配，会产生碎片。增长方式：向高地址。 2. 僵尸对象 空指针 野指针僵尸对象：已经被销毁的对象，内存区域无法再被使用。 空指针：指针无指向，默认nil（0）。">
    <meta http-equiv="Cache-control" content="no-cache">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    
    <link rel="alternate" href="/atom.xml" title="晴风的博客" type="application/atom+xml">
    
    <title>内存管理 · 晴风的博客</title>
    <style type="text/css">
    @font-face {
        font-family: 'Oswald-Regular';
        src: url("/font/Oswald-Regular.ttf");
    }

    body {
        margin: 0;
    }

    header,
    footer,
    .back-top,
    .sidebar,
    .container,
    .site-intro-meta,
    .toc-wrapper {
        display: none;
    }

    .site-intro {
        position: relative;
        z-index: 3;
        width: 100%;
        /* height: 50vh; */
        overflow: hidden;
    }

    .site-intro-placeholder {
        position: absolute;
        z-index: -2;
        top: 0;
        left: 0;
        width: calc(100% + 300px);
        height: 100%;
        background: repeating-linear-gradient(-45deg, #444 0, #444 80px, #333 80px, #333 160px);
        background-position: center center;
        transform: translate3d(-226px, 0, 0);
        animation: gradient-move 2.5s ease-out 0s infinite;
    }

    @keyframes gradient-move {
        0% {
            transform: translate3d(-226px, 0, 0);
        }
        100% {
            transform: translate3d(0, 0, 0);
        }
    }

</style>

    <link rel="preload" href="/css/style.css?v=20180824" as="style" onload="this.onload=null;this.rel='stylesheet'">
    <link rel="stylesheet" href="/css/mobile.css?v=20180824" media="(max-width: 980px)">
    
    <link rel="preload" href="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.2.5/jquery.fancybox.min.css" as="style" onload="this.onload=null;this.rel='stylesheet'">
    
    <!-- /*! loadCSS. [c]2017 Filament Group, Inc. MIT License */
/* This file is meant as a standalone workflow for
- testing support for link[rel=preload]
- enabling async CSS loading in browsers that do not support rel=preload
- applying rel preload css once loaded, whether supported or not.
*/ -->
<script>
(function( w ){
	"use strict";
	// rel=preload support test
	if( !w.loadCSS ){
		w.loadCSS = function(){};
	}
	// define on the loadCSS obj
	var rp = loadCSS.relpreload = {};
	// rel=preload feature support test
	// runs once and returns a function for compat purposes
	rp.support = (function(){
		var ret;
		try {
			ret = w.document.createElement( "link" ).relList.supports( "preload" );
		} catch (e) {
			ret = false;
		}
		return function(){
			return ret;
		};
	})();

	// if preload isn't supported, get an asynchronous load by using a non-matching media attribute
	// then change that media back to its intended value on load
	rp.bindMediaToggle = function( link ){
		// remember existing media attr for ultimate state, or default to 'all'
		var finalMedia = link.media || "all";

		function enableStylesheet(){
			link.media = finalMedia;
		}

		// bind load handlers to enable media
		if( link.addEventListener ){
			link.addEventListener( "load", enableStylesheet );
		} else if( link.attachEvent ){
			link.attachEvent( "onload", enableStylesheet );
		}

		// Set rel and non-applicable media type to start an async request
		// note: timeout allows this to happen async to let rendering continue in IE
		setTimeout(function(){
			link.rel = "stylesheet";
			link.media = "only x";
		});
		// also enable media after 3 seconds,
		// which will catch very old_post browsers (android 2.x, old_post firefox) that don't support onload on link
		setTimeout( enableStylesheet, 3000 );
	};

	// loop through link elements in DOM
	rp.poly = function(){
		// double check this to prevent external calls from running
		if( rp.support() ){
			return;
		}
		var links = w.document.getElementsByTagName( "link" );
		for( var i = 0; i < links.length; i++ ){
			var link = links[ i ];
			// qualify links to those with rel=preload and as=style attrs
			if( link.rel === "preload" && link.getAttribute( "as" ) === "style" && !link.getAttribute( "data-loadcss" ) ){
				// prevent rerunning on link
				link.setAttribute( "data-loadcss", true );
				// bind listeners to toggle media back
				rp.bindMediaToggle( link );
			}
		}
	};

	// if unsupported, run the polyfill
	if( !rp.support() ){
		// run once at least
		rp.poly();

		// rerun poly on an interval until onload
		var run = w.setInterval( rp.poly, 500 );
		if( w.addEventListener ){
			w.addEventListener( "load", function(){
				rp.poly();
				w.clearInterval( run );
			} );
		} else if( w.attachEvent ){
			w.attachEvent( "onload", function(){
				rp.poly();
				w.clearInterval( run );
			} );
		}
	}


	// commonjs
	if( typeof exports !== "undefined" ){
		exports.loadCSS = loadCSS;
	}
	else {
		w.loadCSS = loadCSS;
	}
}( typeof global !== "undefined" ? global : this ) );
</script>

    <link rel="icon" href="/assets/favicon.png">
    <link rel="preload" href="https://cdn.jsdelivr.net/npm/webfontloader@1.6.28/webfontloader.min.js" as="script">
    <link rel="preload" href="https://cdn.jsdelivr.net/npm/jquery@3.3.1/dist/jquery.min.js" as="script">
    <link rel="preload" href="/scripts/main.js" as="script">
    <link rel="preload" as="font" href="/font/Oswald-Regular.ttf" crossorigin>
    <link rel="preload" as="font" href="https://at.alicdn.com/t/font_327081_1dta1rlogw17zaor.woff" crossorigin>
    
    <!-- fancybox -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.2.5/jquery.fancybox.min.js" defer></script>
    <!-- 百度统计  -->
    
    <!-- 谷歌统计  -->
    
<link rel="stylesheet" href="/css/prism-tomorrow.css" type="text/css"></head>

    
        <body class="post-body">
    
    
<header class="header">

    <div class="read-progress"></div>
    <div class="header-sidebar-menu">&#xe775;</div>
    <!-- post页的toggle banner  -->
    
    <div class="banner">
            <div class="blog-title">
                <a href="/" >晴风的博客</a>
            </div>
            <div class="post-title">
                <a href="#" class="post-name">内存管理</a>
            </div>
    </div>
    
    <a class="home-link" href=/>晴风的博客</a>
</header>
    <div class="wrapper">
        <div class="site-intro" style="







height:24vh;
">
    
    <!-- 主页  -->
    
    
    <!-- 404页  -->
            
    <div class="site-intro-placeholder"></div>
    <div class="site-intro-img" style="background-image: url(/intro/index-bg.jpg)"></div>
    <div class="site-intro-meta">
        <!-- 标题  -->
        <h1 class="intro-title">
            <!-- 主页  -->
            
            内存管理
            <!-- 404 -->
            
        </h1>
        <!-- 副标题 -->
        <p class="intro-subtitle">
            <!-- 主页副标题  -->
            
            
            <!-- 404 -->
            
        </p>
        <!-- 文章页meta -->
        
            <div class="post-intros">
                <!-- 文章页标签  -->
                
                    <div class= post-intro-tags >
    
        <a class="post-tag" href="javascript:void(0);" data-tags = "通用">通用</a>
    
</div>
                
                
                    <div class="post-intro-read">
                        <span>字数统计: <span class="post-count word-count">1.8k</span>阅读时长: <span class="post-count reading-time">6 min</span></span>
                    </div>
                
                <div class="post-intro-meta">
                    <span class="post-intro-calander iconfont-archer">&#xe676;</span>
                    <span class="post-intro-time">2020/12/03</span>
                    
                    <span id="busuanzi_container_page_pv" class="busuanzi-pv">
                        <span class="iconfont-archer">&#xe602;</span>
                        <span id="busuanzi_value_page_pv"></span>
                    </span>
                    
                    <span class="shareWrapper">
                        <span class="iconfont-archer shareIcon">&#xe71d;</span>
                        <span class="shareText">Share</span>
                        <ul class="shareList">
                            <li class="iconfont-archer share-qr" data-type="qr">&#xe75b;
                                <div class="share-qrcode"></div>
                            </li>
                            <li class="iconfont-archer" data-type="weibo">&#xe619;</li>
                            <li class="iconfont-archer" data-type="qzone">&#xe62e;</li>
                            <li class="iconfont-archer" data-type="twitter">&#xe634;</li>
                            <li class="iconfont-archer" data-type="facebook">&#xe67a;</li>
                        </ul>
                    </span>
                </div>
            </div>
        
    </div>
</div>
        <script>
 
  // get user agent
  var browser = {
    versions: function () {
      var u = window.navigator.userAgent;
      return {
        userAgent: u,
        trident: u.indexOf('Trident') > -1, //IE内核
        presto: u.indexOf('Presto') > -1, //opera内核
        webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核
        gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, //火狐内核
        mobile: !!u.match(/AppleWebKit.*Mobile.*/), //是否为移动终端
        ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
        android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android终端或者uc浏览器
        iPhone: u.indexOf('iPhone') > -1 || u.indexOf('Mac') > -1, //是否为iPhone或者安卓QQ浏览器
        iPad: u.indexOf('iPad') > -1, //是否为iPad
        webApp: u.indexOf('Safari') == -1, //是否为web应用程序，没有头部与底部
        weixin: u.indexOf('MicroMessenger') == -1, //是否为微信浏览器
        uc: u.indexOf('UCBrowser') > -1 //是否为android下的UC浏览器
      };
    }()
  }
  console.log("userAgent:" + browser.versions.userAgent);

  // callback
  function fontLoaded() {
    console.log('font loaded');
    if (document.getElementsByClassName('site-intro-meta')) {
      document.getElementsByClassName('intro-title')[0].classList.add('intro-fade-in');
      document.getElementsByClassName('intro-subtitle')[0].classList.add('intro-fade-in');
      var postIntros = document.getElementsByClassName('post-intros')[0]
      if (postIntros) {
        postIntros.classList.add('post-fade-in');
      }
    }
  }

  // UC不支持跨域，所以直接显示
  function asyncCb(){
    if (browser.versions.uc) {
      console.log("UCBrowser");
      fontLoaded();
    } else {
      WebFont.load({
        custom: {
          families: ['Oswald-Regular']
        },
        loading: function () {  //所有字体开始加载
          // console.log('loading');
        },
        active: function () {  //所有字体已渲染
          fontLoaded();
        },
        inactive: function () { //字体预加载失败，无效字体或浏览器不支持加载
          console.log('inactive: timeout');
          fontLoaded();
        },
        timeout: 5000 // Set the timeout to two seconds
      });
    }
  }

  function asyncErr(){
    console.warn('script load from CDN failed, will load local script')
  }

  // load webfont-loader async, and add callback function
  function async(u, cb, err) {
    var d = document, t = 'script',
      o = d.createElement(t),
      s = d.getElementsByTagName(t)[0];
    o.src = u;
    if (cb) { o.addEventListener('load', function (e) { cb(null, e); }, false); }
    if (err) { o.addEventListener('error', function (e) { err(null, e); }, false); }
    s.parentNode.insertBefore(o, s);
  }

  var asyncLoadWithFallBack = function(arr, success, reject) {
      var currReject = function(){
        reject()
        arr.shift()
        if(arr.length)
          async(arr[0], success, currReject)
        }

      async(arr[0], success, currReject)
  }

  asyncLoadWithFallBack([
    "https://cdn.jsdelivr.net/npm/webfontloader@1.6.28/webfontloader.min.js", 
    "https://cdn.bootcss.com/webfont/1.6.28/webfontloader.js",
    "/lib/webfontloader.min.js"
  ], asyncCb, asyncErr)
</script>        
        <img class="loading" src="/assets/loading.svg" style="display: block; margin: 6rem auto 0 auto; width: 6rem; height: 6rem;" />
        <div class="container container-unloaded">
            <main class="main post-page">
    <article class="article-entry">
        <h1 id="1-OC-内存管理"><a href="#1-OC-内存管理" class="headerlink" title="1. OC 内存管理"></a>1. OC 内存管理</h1><h2 id="1-堆-和-栈"><a href="#1-堆-和-栈" class="headerlink" title="1. 堆 和 栈"></a>1. 堆 和 栈</h2><p>栈：编译器与操作系统使用，自动释放，入参或者局部变量，先进后出，不会出现内存碎片。增长方式：向低地址</p>
<p>堆：程序员动态分配，非基本数据类型，程序结束是OS回收，但是分配的内存无用后不释放会产生内存泄漏。动态分配，会产生碎片。增长方式：向高地址。</p>
<h2 id="2-僵尸对象-空指针-野指针"><a href="#2-僵尸对象-空指针-野指针" class="headerlink" title="2. 僵尸对象 空指针 野指针"></a>2. 僵尸对象 空指针 野指针</h2><p>僵尸对象：已经被销毁的对象，内存区域无法再被使用。</p>
<p>空指针：指针无指向，默认nil（0）。给空指针发消息不会有任何反应。</p>
<p>野指针：指向僵尸对象，发消息会出现地址访问异常（EXC_BAD_ACCESS）错误。</p>
<h2 id="3-OC-内存管理"><a href="#3-OC-内存管理" class="headerlink" title="3. OC 内存管理"></a>3. OC 内存管理</h2><ul>
<li>分配内存：创建对象</li>
<li>释放内存：销毁对象</li>
</ul>
<p>内存管理范围：继承NSObject，非基本数据类型</p>
<p>内存管理方式三阶段：</p>
<p>①MRC 手动引用计数<br>对象引用计数为零才可以被销毁，除非程序退出<br>使用new alloc init copy MutableCopy 时，对象默认初试RC为1。</p>
<p>发送retain消息，表示持有对象，RC++。</p>
<p>发送release消息，表示不再拥有对象，RC–。</p>
<p>发送retainCount，表示检查RC值，但是（Apple在此处优化-1过程，当retainCount == 1时调用release，该对象一定会被销毁，就不必浪费资源-1为0）不准确，采用dealloc检测内存是否释放</p>
<p>发送dealloc，当一个对象RC为0，会被销毁。也可以验证dealloc是否被调用判断对象是否被销毁</p>
<p>一般重写dealloc方法，并且在方法最后调用父类dealloc，[super dealloc]。</p>
<p>谁创建谁是释放，谁持有谁释放，避免过度释放。</p>
<p>释放后指针成为野指针，记得置空。</p>
<p>@property参数 </p>
<p>生成setter方法</p>
<ul>
<li>retain：release旧值，retain新值（用于对象）</li>
<li>assign：直接赋值，不做内存管理（用于非对象）</li>
<li>copy：release旧值，copy新值（用于NSString不可变）</li>
</ul>
<p>控制生成set方法</p>
<ul>
<li>readwrite get+set</li>
<li>readonly get</li>
</ul>
<p>多线程</p>
<ul>
<li>atomic 默认</li>
<li>nonatomic 性能好</li>
</ul>
<p>循环引用：同时使用retain持有对象内存无法释放，解决办法是一个retain一个assign</p>
<p>②autoreleasepool 自动释放池</p>
<p>运行时，会创建无数个pool，为栈结构，对象调用autorelease时，会入栈。当pool被销毁时，对pool的对象发送release，如果RC 为0，则对象被销毁，不为0则不会被销毁。</p>
<p>③ARC 自动引用计数<br>自动引用计数，不允许使用release方法，重写dealloc不能调用父类dealloc。<br>ARC是编译器特性，不是其他语言的动态特性GC。</p>
<p>局部变量释放，对象随之释放。<br>清空指针，对象随之释放。（例如a = nil）<br>默认清空指针是strong的<br>不要使用weak保存新创建的对象</p>
<p>循环引用问题：一个strong一个weak</p>
<p>@property参数</p>
<p>strong ≈ retain<br>weak ≈ assign</p>
<h1 id="2-C-内存管理"><a href="#2-C-内存管理" class="headerlink" title="2. C++ 内存管理"></a>2. C++ 内存管理</h1><h2 id="1-内存分区"><a href="#1-内存分区" class="headerlink" title="1. 内存分区"></a>1. 内存分区</h2><p>①动态存储区：堆（new 对象初始化，delete清除，开发控制） 栈（局部变量，OS 编译器 控制）（分为动态与静态分配，编译器负责分配，动态alloc函数分配）</p>
<p>②静态区（static全局变量）c语言区分初始化与非初始化</p>
<p>③自由存储区（malloc分配的，free 清除）</p>
<p>④常量区（不变量）</p>
<p>⑤代码区</p>
<p>常见内存出错：</p>
<ul>
<li>内存分配未成功（记得检查指针不为空）</li>
<li>未初始化（最好赋零值，缺省初值不定）</li>
<li>越界（常见的数组边界）</li>
<li>内存泄漏（忘记销毁对象释放内存，new与delete成对出现）</li>
<li>野指针（释放了内存还在使用内存区域）</li>
</ul>
<p>strcpy 复制数组<br>strcmp 比较数组<br>复制arr内容，先malloc内存strlen(arr)区域，然后strcpy</p>
<p>参考了 <a href="https://chenqx.github.io/2014/09/25/Cpp-Memory-Management/" target="_blank" rel="noopener">C/C++内存管理详解</a></p>
<h1 id="3-Java-内存管理"><a href="#3-Java-内存管理" class="headerlink" title="3. Java 内存管理"></a>3. Java 内存管理</h1><h2 id="1-内存分区-1"><a href="#1-内存分区-1" class="headerlink" title="1.内存分区"></a>1.内存分区</h2><p>①方法区：存放加载类的信息、类的静态变量、final变量、类的方法。全局共享，持久代。本区域的垃圾回收主要是常量池与已加载类的卸载。<br>运行时常量池：是方法区的部分，存储编译器生成的字面常量、符号引用、直接引用。<br>–XX：（PermSize/MaxPermSize）最大最小</p>
<p>②堆：GC机制重点关注区域，最大的一块内存区，所有线程共享。虚拟机启东市创建。用来存储对象实例与数组值。可以认为new创建的对象都在此处分配。</p>
<p>–Xms：JVM启动时申请的最小内存，默认1/64 &amp;&amp; &lt; 1G。</p>
<p>–Xmx：JVM可申请的最大内存，默认1/4 &amp;&amp; &lt; 1G。</p>
<p>为了内存回收高效，sun jdk 1.2开始才去分代的管理方式。（young old permanent）</p>
<p>年轻代按比例分为eden（复制回收） 和 survivor，回收年轻代内存称为minor gc</p>
<p>内存首先在年轻代分配对象，如果对象大，直接分配到老年代</p>
<p>老年代指经过多次垃圾回收仍然存活的对象，老年代内存回收称为major gc</p>
<p>③本地方法栈（多为c）</p>
<p>存储native方法，与虚拟机栈运行机制一致，虚拟机栈执行java方法。</p>
<p>④虚拟机栈（JVM栈）</p>
<p>占用操作系统内存，线程私有，类似于C++的栈，存储局部变量等</p>
<p>⑤PC</p>
<p>记录字节码执行位置，为线程行号指示器</p>
<h2 id="2-对象回收判定算法"><a href="#2-对象回收判定算法" class="headerlink" title="2. 对象回收判定算法"></a>2. 对象回收判定算法</h2><ul>
<li>引用计数<br>每个对象拥有一个引用计数器。普遍存在循环引用问题。</li>
<li>可达性分析<br>root对象作为起点，进行深度搜索，走过的路径称为引用链，当链不经过root时，表明从root到当前对象不可达，标记为可回收。java中可以作为root的对象有（虚拟机栈、native方法栈native方法引用的对象、方法区静态属性引用的对象、方法区常量引用的对象）<br>// TODO</li>
</ul>
<h2 id="3-垃圾收集算法"><a href="#3-垃圾收集算法" class="headerlink" title="3. 垃圾收集算法"></a>3. 垃圾收集算法</h2><ul>
<li><p>标记清除<br>原位置删除，容易产生碎片。标记清除效率不高。</p>
</li>
<li><p>复制<br>开辟一半区域，存活的对象复制到新区域。解决碎片，但是空间利用率减半。</p>
</li>
<li><p>标记整理<br>存活对象向一侧移动，清除掉端边界以外的内存。</p>
</li>
<li><p>分代收集<br>GC大都采用分代收集，新生代与老年代，新版本永久代废弃。引入元空间，直接使用物理内存。<br>新生代分三个区（eden survivor from survivor to）默认811。<br>优先使用eden 与 from，用满后，进行GC，（停止复制法）存活的copy到to并清除eden与from，如果to无法容纳，copy到老年代，再无法容纳，full gc。</p>
</li>
</ul>
<h2 id="4-收集器"><a href="#4-收集器" class="headerlink" title="4. 收集器"></a>4. 收集器</h2><ul>
<li><p>串行收集器</p>
</li>
<li><p>并行收集器</p>
</li>
<li><p>CMS（用于永久代）</p>
</li>
<li><p>G1收集器（并发 增量压缩 暂停时间段）</p>
</li>
</ul>
<h2 id="5-HotSpot虚拟机"><a href="#5-HotSpot虚拟机" class="headerlink" title="5. HotSpot虚拟机"></a>5. HotSpot虚拟机</h2><p>垃圾收集器是垃圾收集算法的实现。依据HotSpot虚拟机。</p>
<p>参考了<br><a href="https://blog.csdn.net/suifeng3051/article/details/48292193" target="_blank" rel="noopener">JVM内存管理及GC机制</a><br><a href="https://zhuanlan.zhihu.com/p/38056939" target="_blank" rel="noopener">JVM自动内存管理机制</a></p>

    </article>
    <!-- license  -->
    
        <div class="license-wrapper">
            <p>原文作者：<a href="https://blog.xq99.org">XQ</a>
            <p>原文链接：<a href="https://blog.xq99.org/posts/2.内存管理/">https://blog.xq99.org/posts/2.内存管理/</a>
            <p>发表日期：<a href="https://blog.xq99.org/posts/2.内存管理/">December 3rd 2020, 2:18:09 pm</a>
            <p>更新日期：<a href="https://blog.xq99.org/posts/2.内存管理/">December 3rd 2020, 2:18:13 pm</a>
            <p>版权声明：本文采用<a rel="license" href="http://creativecommons.org/licenses/by-nc/4.0/">知识共享署名-非商业性使用 4.0 国际许可协议</a>进行许可</p>
        </div>
    
    <!-- paginator  -->
    <ul class="post-paginator">
        <li class="next">
            
                <div class="nextSlogan">Next Post</div>
                <a href= "/posts/3.自我驱动/" title= "自我驱动">
                    <div class="nextTitle">自我驱动</div>
                </a>
            
        </li>
        <li class="previous">
            
                <div class="prevSlogan">Previous Post</div>
                <a href= "/posts/1.C++学习的四个层次/" title= "C++学习的四个层次">
                    <div class="prevTitle">C++学习的四个层次</div>
                </a>
            
        </li>
    </ul>
    <!-- 评论插件 -->
    <!-- 来必力City版安装代码 -->

<!-- City版安装代码已完成 -->
    
    <div id="disqus_thread"></div>
    <script>
        /**
    *  RECOMMENDED CONFIGURATION VARIABLES: EDIT AND UNCOMMENT THE SECTION BELOW TO INSERT DYNAMIC VALUES FROM YOUR PLATFORM OR CMS.
    *  LEARN WHY DEFINING THESE VARIABLES IS IMPORTANT: https://disqus.com/admin/universalcode/#configuration-variables*/
        
        var disqus_config = function () {
        this.page.url = "https://blog.xq99.org/posts/2.内存管理/";  // Replace PAGE_URL with your page's canonical URL variable
        this.page.identifier = "内存管理"; // Replace PAGE_IDENTIFIER with your page's unique identifier variable
        };
        
        (function () { // DON'T EDIT BELOW THIS LINE
            var d = document, s = d.createElement('script');
            s.src = 'https://xq773939719.disqus.com/embed.js';
            s.setAttribute('data-timestamp', +new Date());
            (d.head || d.body).appendChild(s);
        })();

    </script>
    <noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>

    
    <!-- gitalk评论 -->

    <!-- utteranc评论 -->

    <!-- partial('_partial/comment/changyan') -->
    <!--PC版-->


    
    

    <!-- 评论 -->
</main>
            <!-- profile -->
            
        </div>
        <footer class="footer footer-unloaded">
    <!-- social  -->
    
    <div class="social">
        
    
        
            
                <a href="mailto:773939719@qq.com" class="iconfont-archer email" title=email ></a>
            
        
    
        
            
                <a href="//github.com/xq773939719" class="iconfont-archer github" target="_blank" title=github></a>
            
        
    
        
            
                <span class="iconfont-archer wechat" title=wechat>
                  
                  <img class="profile-qr" src="/assets/wechat.jpeg" />
                </span>
            
        
    
        
            
                <span class="iconfont-archer qq" title=qq>
                  
                  <img class="profile-qr" src="/assets/qq.jpeg" />
                </span>
            
        
    
        
    
        
    
        
            
                <a href="https://www.zhihu.com/people/773939719" class="iconfont-archer zhihu" target="_blank" title=zhihu></a>
            
        
    
        
    
        
    
        
            
                <a href="https://twitter.com/xq773939719" class="iconfont-archer twitter" target="_blank" title=twitter></a>
            
        
    
        
    
        
    
        
    
        
            
                <a href="https://juejin.im/user/2735240659087127" class="iconfont-archer juejin" target="_blank" title=juejin></a>
            
        
    
        
    
        
            
                <a href="https://space.bilibili.com/7076447" class="iconfont-archer bilibili" target="_blank" title=bilibili></a>
            
        
    
        
            
                <a href="https://www.linkedin.com/in/强-许-013122119/" class="iconfont-archer linkedin" target="_blank" title=linkedin></a>
            
        
    
        
    
        
    
        
    

    </div>
    
    <!-- powered by Hexo  -->
    <div class="copyright">
        <span id="hexo-power">Powered by <a href="https://hexo.io/" target="_blank">Hexo</a></span><span class="iconfont-archer power">&#xe635;</span><span id="theme-info">theme <a href="https://github.com/fi3ework/hexo-theme-archer" target="_blank">Archer</a></span>
    </div>
    <!-- 不蒜子  -->
    
    <div class="busuanzi-container">
    
     
    <span id="busuanzi_container_site_pv">PV: <span id="busuanzi_value_site_pv"></span> :)</span>
    
    </div>
    
</footer>
    </div>
    <!-- toc -->
    
    <div class="toc-wrapper" style=
    







top:24vh;

    >
        <div class="toc-catalog">
            <span class="iconfont-archer catalog-icon">&#xe613;</span><span>CATALOG</span>
        </div>
        <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#1-OC-内存管理"><span class="toc-number">1.</span> <span class="toc-text">1. OC 内存管理</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#1-堆-和-栈"><span class="toc-number">1.1.</span> <span class="toc-text">1. 堆 和 栈</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-僵尸对象-空指针-野指针"><span class="toc-number">1.2.</span> <span class="toc-text">2. 僵尸对象 空指针 野指针</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-OC-内存管理"><span class="toc-number">1.3.</span> <span class="toc-text">3. OC 内存管理</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#2-C-内存管理"><span class="toc-number">2.</span> <span class="toc-text">2. C++ 内存管理</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#1-内存分区"><span class="toc-number">2.1.</span> <span class="toc-text">1. 内存分区</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#3-Java-内存管理"><span class="toc-number">3.</span> <span class="toc-text">3. Java 内存管理</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#1-内存分区-1"><span class="toc-number">3.1.</span> <span class="toc-text">1.内存分区</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-对象回收判定算法"><span class="toc-number">3.2.</span> <span class="toc-text">2. 对象回收判定算法</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-垃圾收集算法"><span class="toc-number">3.3.</span> <span class="toc-text">3. 垃圾收集算法</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-收集器"><span class="toc-number">3.4.</span> <span class="toc-text">4. 收集器</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5-HotSpot虚拟机"><span class="toc-number">3.5.</span> <span class="toc-text">5. HotSpot虚拟机</span></a></li></ol></li></ol>
    </div>
    
    <div class="back-top iconfont-archer">&#xe639;</div>
    <div class="sidebar sidebar-hide">
    <ul class="sidebar-tabs sidebar-tabs-active-0">
        <li class="sidebar-tab-archives"><span class="iconfont-archer">&#xe67d;</span><span class="tab-name">Archive</span></li>
        <li class="sidebar-tab-tags"><span class="iconfont-archer">&#xe61b;</span><span class="tab-name">Tag</span></li>
        <li class="sidebar-tab-categories"><span class="iconfont-archer">&#xe666;</span><span class="tab-name">Cate</span></li>
    </ul>
    <div class="sidebar-content sidebar-content-show-archive">
          <div class="sidebar-panel-archives">
    <!-- 在ejs中将archive按照时间排序 -->
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    <div class="total-and-search">
        <div class="total-archive">
        Total : 18
        </div>
        <!-- search  -->
        
    </div>
    
    <div class="post-archive">
    
    
    
    
    <div class="archive-year"> 2020 </div>
    <ul class="year-list">
    
    
        <li class="archive-post-item">
            <span class="archive-post-date">12/03</span><a class="archive-post-title" href= "/posts/18.阅读进度/" >阅读进度</a>
        </li>
    
    
        <li class="archive-post-item">
            <span class="archive-post-date">12/03</span><a class="archive-post-title" href= "/posts/13.前端学习网站/" >前端学习网站</a>
        </li>
    
    
        <li class="archive-post-item">
            <span class="archive-post-date">12/03</span><a class="archive-post-title" href= "/posts/14.算法学习网站/" >算法学习网站</a>
        </li>
    
    
        <li class="archive-post-item">
            <span class="archive-post-date">12/03</span><a class="archive-post-title" href= "/posts/15.项目学习网站/" >项目学习网站</a>
        </li>
    
    
        <li class="archive-post-item">
            <span class="archive-post-date">12/03</span><a class="archive-post-title" href= "/posts/16.移动端学习网站/" >移动端学习网站</a>
        </li>
    
    
        <li class="archive-post-item">
            <span class="archive-post-date">12/03</span><a class="archive-post-title" href= "/posts/17.OpenGL学习/" >OpenGL学习</a>
        </li>
    
    
        <li class="archive-post-item">
            <span class="archive-post-date">12/03</span><a class="archive-post-title" href= "/posts/2.内存管理/" >内存管理</a>
        </li>
    
    
        <li class="archive-post-item">
            <span class="archive-post-date">12/03</span><a class="archive-post-title" href= "/posts/3.自我驱动/" >自我驱动</a>
        </li>
    
    
        <li class="archive-post-item">
            <span class="archive-post-date">12/03</span><a class="archive-post-title" href= "/posts/4.计算机网络浅谈/" >计算机网络浅谈</a>
        </li>
    
    
        <li class="archive-post-item">
            <span class="archive-post-date">12/03</span><a class="archive-post-title" href= "/posts/5.Git命令/" >Git命令</a>
        </li>
    
    
        <li class="archive-post-item">
            <span class="archive-post-date">12/03</span><a class="archive-post-title" href= "/posts/6.红黑树的实现/" >红黑树的实现</a>
        </li>
    
    
        <li class="archive-post-item">
            <span class="archive-post-date">12/03</span><a class="archive-post-title" href= "/posts/8.深度探索C++对象模型/" >深度探索C++对象模型</a>
        </li>
    
    
        <li class="archive-post-item">
            <span class="archive-post-date">12/03</span><a class="archive-post-title" href= "/posts/9.iOS开发知识点集合/" >iOS开发知识集合</a>
        </li>
    
    
        <li class="archive-post-item">
            <span class="archive-post-date">12/03</span><a class="archive-post-title" href= "/posts/10.读《如何自学Android》有感/" >读《如何自学Android》有感</a>
        </li>
    
    
        <li class="archive-post-item">
            <span class="archive-post-date">12/03</span><a class="archive-post-title" href= "/posts/11.NPM与Yarn源管理工具/" >NPM与Yarn源管理工具</a>
        </li>
    
    
        <li class="archive-post-item">
            <span class="archive-post-date">12/03</span><a class="archive-post-title" href= "/posts/12.后端学习网站/" >后端学习网站</a>
        </li>
    
    
        <li class="archive-post-item">
            <span class="archive-post-date">12/03</span><a class="archive-post-title" href= "/posts/7.收藏夹/" >收藏夹</a>
        </li>
    
    
        <li class="archive-post-item">
            <span class="archive-post-date">12/03</span><a class="archive-post-title" href= "/posts/1.C++学习的四个层次/" >C++学习的四个层次</a>
        </li>
    
    </div>
  </div>
        <div class="sidebar-panel-tags">
    <div class="sidebar-tags-name">
    
        <span class="sidebar-tag-name" data-tags="RN"><span class="iconfont-archer">&#xe606;</span>RN</span>
    
        <span class="sidebar-tag-name" data-tags="iOS"><span class="iconfont-archer">&#xe606;</span>iOS</span>
    
        <span class="sidebar-tag-name" data-tags="阅读"><span class="iconfont-archer">&#xe606;</span>阅读</span>
    
        <span class="sidebar-tag-name" data-tags="C++"><span class="iconfont-archer">&#xe606;</span>C++</span>
    
        <span class="sidebar-tag-name" data-tags="学习"><span class="iconfont-archer">&#xe606;</span>学习</span>
    
        <span class="sidebar-tag-name" data-tags="环境"><span class="iconfont-archer">&#xe606;</span>环境</span>
    
        <span class="sidebar-tag-name" data-tags="软实力"><span class="iconfont-archer">&#xe606;</span>软实力</span>
    
        <span class="sidebar-tag-name" data-tags="通用"><span class="iconfont-archer">&#xe606;</span>通用</span>
    
        <span class="sidebar-tag-name" data-tags="网络"><span class="iconfont-archer">&#xe606;</span>网络</span>
    
        <span class="sidebar-tag-name" data-tags="HTTP"><span class="iconfont-archer">&#xe606;</span>HTTP</span>
    
        <span class="sidebar-tag-name" data-tags="HTTPS"><span class="iconfont-archer">&#xe606;</span>HTTPS</span>
    
        <span class="sidebar-tag-name" data-tags="数据结构"><span class="iconfont-archer">&#xe606;</span>数据结构</span>
    
        <span class="sidebar-tag-name" data-tags="红黑树"><span class="iconfont-archer">&#xe606;</span>红黑树</span>
    
        <span class="sidebar-tag-name" data-tags="收藏夹"><span class="iconfont-archer">&#xe606;</span>收藏夹</span>
    
        <span class="sidebar-tag-name" data-tags="对象"><span class="iconfont-archer">&#xe606;</span>对象</span>
    
        <span class="sidebar-tag-name" data-tags="Android"><span class="iconfont-archer">&#xe606;</span>Android</span>
    
        <span class="sidebar-tag-name" data-tags="源管理"><span class="iconfont-archer">&#xe606;</span>源管理</span>
    
        <span class="sidebar-tag-name" data-tags="后端"><span class="iconfont-archer">&#xe606;</span>后端</span>
    
        <span class="sidebar-tag-name" data-tags="前端"><span class="iconfont-archer">&#xe606;</span>前端</span>
    
        <span class="sidebar-tag-name" data-tags="算法"><span class="iconfont-archer">&#xe606;</span>算法</span>
    
        <span class="sidebar-tag-name" data-tags="项目"><span class="iconfont-archer">&#xe606;</span>项目</span>
    
        <span class="sidebar-tag-name" data-tags="移动端"><span class="iconfont-archer">&#xe606;</span>移动端</span>
    
        <span class="sidebar-tag-name" data-tags="音视频"><span class="iconfont-archer">&#xe606;</span>音视频</span>
    
        <span class="sidebar-tag-name" data-tags="OpenGL"><span class="iconfont-archer">&#xe606;</span>OpenGL</span>
    
        <span class="sidebar-tag-name" data-tags="网站"><span class="iconfont-archer">&#xe606;</span>网站</span>
    
    </div>
    <div class="iconfont-archer sidebar-tags-empty">&#xe678;</div>
    <div class="tag-load-fail" style="display: none; color: #ccc; font-size: 0.6rem;">
    缺失模块。<br/>
    1、请确保node版本大于6.2<br/>
    2、在博客根目录（注意不是archer根目录）执行以下命令：<br/>
    <span style="color: #f75357; font-size: 1rem; line-height: 2rem;">npm i hexo-generator-json-content --save</span><br/>
    3、在根目录_config.yml里添加配置：
    <pre style="color: #787878; font-size: 0.6rem;">
jsonContent:
  meta: false
  pages: false
  posts:
    title: true
    date: true
    path: true
    text: false
    raw: false
    content: false
    slug: false
    updated: false
    comments: false
    link: false
    permalink: false
    excerpt: false
    categories: true
    tags: true</pre>
    </div> 
    <div class="sidebar-tags-list"></div>
</div>
        <div class="sidebar-panel-categories">
    <div class="sidebar-categories-name">
    
        <span class="sidebar-category-name" data-categories="技术"><span class="iconfont-archer">&#xe60a;</span>技术</span>
    
        <span class="sidebar-category-name" data-categories="技术/移动端"><span class="iconfont-archer">&#xe60a;</span>技术/移动端</span>
    
        <span class="sidebar-category-name" data-categories="技术/博客"><span class="iconfont-archer">&#xe60a;</span>技术/博客</span>
    
        <span class="sidebar-category-name" data-categories="生活"><span class="iconfont-archer">&#xe60a;</span>生活</span>
    
    </div>
    <div class="iconfont-archer sidebar-categories-empty">&#xe678;</div>
    <div class="sidebar-categories-list"></div>
</div>
    </div>
</div> 
    <script>
    var siteMeta = {
        root: "/",
        author: "XQ"
    }
</script>
    <!-- CDN failover -->
    <script src="https://cdn.jsdelivr.net/npm/jquery@3.3.1/dist/jquery.min.js"></script>
    <script type="text/javascript">
        if (typeof window.$ === 'undefined')
        {
            console.warn('jquery load from jsdelivr failed, will load local script')
            document.write('<script src="/lib/jquery.min.js">\x3C/script>')
        }
    </script>
    <script src="/scripts/main.js"></script>
    <!-- algolia -->
    
    <!-- busuanzi  -->
    
    <script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    
    <!-- CNZZ  -->
    
    </div>
    <!-- async load share.js -->
    
        <script src="/scripts/share.js" async></script>    
     
    </body>
</html>


